package com.zhangh.summer.admin.support.prometheus.common.client;

import io.prometheus.client.Histogram;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

/**
 * @description: TODO
 * @author: zhanghui  zhanghlucky@163.com
 * @create: 2020-08-26 15:03
 **/
@Slf4j
public class HistogramClient {
    private Map<String, Histogram> HISTOGRAM_MAP=new HashMap<>();

    public HistogramClient register(String name,String help,double[] buckets,String[] labelNames){
        if (null==HISTOGRAM_MAP.get(name)) {
            Histogram client = Histogram.build().
                    name(name).
                    labelNames(labelNames).
                    help(help).
                    buckets(buckets).
                    register();
            HISTOGRAM_MAP.put(name,client);
            log.info("Metrics Histogram register name:{},help:{},buckets:{},labelNames;{}",name,help,buckets,labelNames);
        }else{
            log.warn("Metrics Histogram exist name:{}",name);
        }
        return this;
    }
    public HistogramClient register(String name,String help,String... labelNames){
        if (null==HISTOGRAM_MAP.get(name)) {
            Histogram client = Histogram.build().
                    name(name).
                    labelNames(labelNames).
                    help(help).
                    register();
            HISTOGRAM_MAP.put(name,client);
            log.info("Metrics Histogram register name:{},help:{},labelNames;{}",name,help,labelNames);
        }else{
            log.warn("Metrics Histogram exist name:{}",name);
        }
        return this;
    }
    public HistogramClient register(String name, Histogram client){
        if (null==HISTOGRAM_MAP.get(name)) {
            client.register();
            HISTOGRAM_MAP.put(name,client);
            log.info("Metrics Histogram register name:{}",name);
        }else{
            log.warn("Metrics Histogram exist name:{}",name);
        }
        return this;
    }
    public Histogram get(String name){
        Histogram client=HISTOGRAM_MAP.get(name);
        if (null!=client){
            return client;
        }
        log.error("can not find counter:{} ,please register it",name);
        return null;
    }
}
